<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" :title="'流程上传部署' + '(' + form.busMainData.flowName + ')'" width="600px">
    <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="160px">
      <el-form-item label="上传文件" prop="attachmentList">
        <FileUpload
          ref="fileUpload"
          :sup_this="sup_this"
          :file-limit="1"
          :is-direct-upload="false"
          accept=".bpmn" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { importModel } from '@/api/system/workflowDef'
import FileUpload from '@/views/components/FileUpload'

export default {
  components: { FileUpload },
  props: {
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    const validFile = (rule, value, callback) => {
      if (this.form.attachmentList && this.form.attachmentList.length === 0) {
        callback(new Error('请上传文件'))
      } else if (this.form.attachmentList && this.form.attachmentList.length !== 1) {
        callback(new Error('只上传1个bpmn文件'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      dialog: false,
      style: 'width: 184px',
      form: {
        attachmentList: [],
        busMainData: {
          enableFlag: 'Y'
        },
        busCommonState: {
          selfState: '10' // 默认已生效
        }
      },
      rules: {
        'attachmentList': [
          { required: true, trigger: 'blur', validator: validFile }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initLoadComponent(row) {
      this.form.busMainData = row
    },
    doSubmit() {
      this.form.attachmentList = this.$refs.fileUpload.getFileList()
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.importModel()
        }
      })
    },
    importModel() {
      importModel(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '上传部署成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>
